package com.xtourms.client.frame;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.xtourms.client.Context;
import com.xtourms.client.dep.DepartmentFrame;
import com.xtourms.client.main.IndexFrame;
import com.xtourms.client.om.OrderManagerFrame;
import com.xtourms.client.rm.ResManagerFrame;
import com.xtourms.client.sys.SystemManagerFrame;
import com.xtourms.client.sys.um.LoginPanel;

public class MainFrame extends Composite {

	private static MainFrameUiBinder uiBinder = GWT
			.create(MainFrameUiBinder.class);

	interface MainFrameUiBinder extends UiBinder<Widget, MainFrame> {
	}

	private static final MainFrame ins = new MainFrame();
	
	private Widget cur_widget;
	private Anchor cur_anchor;
	
	@UiField HTMLPanel mainframe;
	@UiField Anchor index;
	@UiField Anchor res;
	@UiField Anchor sys;
	@UiField Anchor dep;
	@UiField Anchor order;
	
	public MainFrame() {
		initWidget(uiBinder.createAndBindUi(this));
		cur_anchor = index;
		cur_anchor.addStyleName("cur_nav");
		cur_widget = new IndexFrame();
		mainframe.add(cur_widget, "mainframe_content");
	}

	public static MainFrame getIns(){
		return ins;
	}

	public void reInit() {
		//TODO:
	}
	
	@UiHandler("index")
	void handleIndex(ClickEvent evt){
		switchNav(index, new IndexFrame());
	}

	@UiHandler("res")
	void handleRes(ClickEvent evt){
		switchNav(res, new ResManagerFrame());
		
	}
	
	@UiHandler("sys")
	void handleSys(ClickEvent evt){
		switchNav(sys, new SystemManagerFrame());
	}
	
	@UiHandler("order")
	void handleOrder(ClickEvent evt){
		switchNav(order, new OrderManagerFrame());
	}
	
	@UiHandler("dep")
	void showDep(ClickEvent evt){
		switchNav(dep, new DepartmentFrame());
	}
	
	@UiHandler("exit")
	void handleExit(ClickEvent evt){
		Context.ins.clear();
		RootPanel.get().clear();
		RootPanel.get().add(new LoginPanel());
	}
	
	private void switchNav(Anchor nextAnchor, Widget nextContent){
		cur_anchor.removeStyleName("cur_nav");
		cur_anchor = nextAnchor;
		cur_anchor.addStyleName("cur_nav");		
		cur_widget.removeFromParent();
		cur_widget = nextContent;
		mainframe.add(cur_widget, "mainframe_content");
	}
}
